import axios from 'axios';
import { Modal, Message, Notification } from "@arco-design/web-vue";
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
const instance = axios.create({
  baseURL: import.meta.env.VITE_API_BASE_URL,
  timeout: 120000,
});

// 请求和响应拦截器保持不变
instance.interceptors.request.use(
  (config) => {
    // const token = localStorage.getItem('token');
    // if (token) {
    //   config.headers.Authorization = `Bearer ${token}`;
    // }
    return config;
  },
  (error) => Promise.reject(error)
);

instance.interceptors.response.use(
  (res) => {
    // console.log('请求拦截', res)
    const code = res.data.code || 200;
    if (code != 200) {
      Message.error(res.message)
      return Promise.reject(new Error(res.message))
    } else {
      return Promise.resolve(res.data)
    }
  },
  (error) => {
    let { message } = error;
    if (message == "Network Error") {
      message = "后端接口连接异常";
    } else if (message.includes("timeout")) {
      message = "系统接口请求超时";
    } else if (message.includes("Request failed with status code")) {
      message = "系统接口" + message.substr(message.length - 3) + "异常";
    }
    Message.error({ content: message, duration: 5 * 1000 })
    return Promise.reject(error)
  }
);

export default instance;
